package org.jfree.chart.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Sabahul & alex
 *
 */

public class Persistance {

	
	public static List sensorReading(){
		
		Connection connection;
		List<SensorDomain> sensorsDomains = new ArrayList<SensorDomain>();

		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sns", "root", "root");

			PreparedStatement stmt = connection.prepareStatement("SELECT crTime, sensorR FROM sns.snsTable Order by crTime DESC LIMIT 20;");
			ResultSet resultSet = stmt.executeQuery();
			
			while(resultSet.next()){
				SensorDomain sensorDomain = new SensorDomain();
				sensorDomain.setTimeStamp(resultSet.getString(1));
				sensorDomain.setSensorReading(resultSet.getString(2));
				sensorsDomains.add(sensorDomain);
			}
			
			stmt.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
//		XXX
		return sensorsDomains;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		Connection connection;
//
//		try {
////			Class.forName("com.mysql.jdbc.Driver").newInstance();
//			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sns", "root", "root");
//
//			Statement stmt = connection.createStatement();
//			Date date = new Date();
//			System.err.println(date);
//			Double lightValue = 3.9;
//			
//			
//			String insertSQL = "insert into sns.snsTable(id, sensorR) values(1 " + ", " + lightValue + ");";
//			stmt.executeUpdate(insertSQL);
//			stmt.close();
//			connection.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		Connection connection;
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sns", "root", "root");

			PreparedStatement stmt = connection.prepareStatement("SELECT crTime, sensorR FROM sns.snsTable Order by crTime DESC LIMIT 20;");
			ResultSet resultSet = stmt.executeQuery();
			
			while(resultSet.next()){
				System.out.println("1: " + resultSet.getString(1));
				System.out.println("2: " + resultSet.getString(2));
			}
			stmt.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
